package com.alibaba.middleware.hsf.guide.io;

import com.taobao.hsf.invocation.Invocation;
import com.taobao.hsf.invocation.RPCResult;
import com.taobao.hsf.io.client.Client;
import com.taobao.hsf.io.common.RemotingConstants;
import com.taobao.hsf.io.serialize.SerializeType;
import com.taobao.hsf.io.stream.ClientStream;
import com.taobao.hsf.model.metadata.MethodSpecial;
import com.taobao.hsf.protocol.ConnectionSupport;
import com.taobao.hsf.protocol.MultiConnectionSupport;
import com.taobao.hsf.protocol.ServiceURL;
import com.taobao.hsf.util.AttributeMap;
import com.taobao.hsf.util.HSFServiceContainer;
import com.taobao.hsf.util.concurrent.ListenableFuture;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * @author weipeng2k 2017年04月12日 下午15:39:34
 */
public class StreamInvoke {

    static Client client = HSFServiceContainer.getInstance(Client.class);

    public static void main(String[] args) {
        ServiceURL serviceURL = new TestServiceURL();
        ClientStream of = client.of(serviceURL);
        Invocation invocation = new Invocation();
        invocation.setTargetServiceUniqueName("com.alibaba.middleware.hsf.guide.api.service.OrderService:1.0.0");
        invocation.setMethodName("queryOrder");
        invocation.setMethodArgSigs(new String[]{Long.class.getName()});
        invocation.setMethodArgs(new Object[]{10L});

        invocation.getInvokerContext().setSerializeType(SerializeType.HESSIAN2.getCode());
        invocation.getInvokerContext().setTimeout(3000);
        invocation.getInvokerContext().setProtocolType(serviceURL.getProtocolType());

        final ListenableFuture<RPCResult> write = of.write(invocation);
        write.addListener(new Runnable() {
            @Override
            public void run() {
                try {
                    RPCResult rpcResult = write.get();
                    String reflectionToString = ToStringBuilder.reflectionToString(rpcResult,
                            ToStringStyle.MULTI_LINE_STYLE);
                    System.out.println(reflectionToString);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    static class TestServiceURL implements ServiceURL {

        private MultiConnectionSupport multiConnectionSupport = new MultiConnectionSupport(this, 16);

        @Override
        public String getProtocol() {
            return null;
        }

        @Override
        public String getHost() {
            return "11.239.175.138";
        }

        @Override
        public int getPort() {
            return 12200;
        }

        @Override
        public String getPath() {
            return null;
        }

        @Override
        public String getParameterString() {
            return null;
        }

        @Override
        public String getParameter(String key) {
            return "4000";
        }

        @Override
        public boolean hasParameter(String key) {
            return false;
        }

        @Override
        public String getUrl() {
            return null;
        }

        @Override
        public String getUrlWithoutProtocol() {
            return null;
        }

        @Override
        public ConnectionSupport getConnectionSupport() {
            return multiConnectionSupport;
        }

        @Override
        public int getTimeout() {
            return 0;
        }

        @Override
        public byte getSerializeType() {
            return 0;
        }

        @Override
        public List<MethodSpecial> getMethodSpecials() {
            return null;
        }

        @Override
        public int getParameter(String parameterName, int defaultValue) {
            return 0;
        }

        @Override
        public String getParameter(String key, String defaultValue) {
            return null;
        }

        @Override
        public String getServerTypeName() {
            return "HSF2";
        }

        @Override
        public byte getProtocolType() {
            return RemotingConstants.PROTOCOL_VERSION_HSF_REMOTING;
        }

        @Override
        public AttributeMap getAttributeMap() {
            return null;
        }
    }
}
